<template>
  <div class="order-confirm page" ref="webview">
    <div ref="wrapper">
      <div class="hd-bar">
        <van-nav-bar title="订单结算" left-arrow @click-left="$router.go(-1)" class="bg_white" />
      </div>
      <div class="content">
        <template v-if="goodsInfo && goodsInfo.goods && !goodsInfo.goods.is_virtual">
          <div class="address-box" @click="goAddress">
            <div class="detail">
              <template v-if="address">
                <div class="name-phone">{{address.name}} {{address.mobile | phoneFilter}}（收）</div>
                <div class="address">{{address.prov_name}}{{address.city_name}}{{address.area_name}} {{address.address}}</div>
              </template>
              <template v-else>
                请添加收货址
              </template>
            </div>
            <div class="next"></div>
          </div>
        </template>
        <div class="product-box">
          <div class="product-info">
            <div class="imgPath">
              <van-image width="100%" :height="$tools.toPX(180)" fit="contain" lazy-load :src="goods && goods.goods_img">
                <template v-slot:loading>
                  <van-loading type="spinner" size="24" />
                </template>
              </van-image>
            </div>
            <div class="detail">
              <div class="name">{{goods && goods.goods_name}}</div>
              <div class="spec">{{goods && goods.norm_name}}</div>
              <div class="price-num">
                <div class="price">￥{{goods && goods.norm_price}}
                  <!-- <span class="text">配送积分</span> -->
                </div>
              </div>
            </div>
          </div>
          <div class="inventory">
            <div class="item-title">数量</div>
            <div class="item-info">
              <van-stepper v-model="goods.num" min="1" :max="stock" />
            </div>
          </div>
          <!-- <div class="remark">
            <van-field v-model="remark" rows="1" autosize type="textarea" placeholder="备注…" />
          </div> -->
        </div>

      </div>
      <div class="bt-box">
        <div class="info">
          <span>共{{goods && goods.num}}件，</span>
          <span>实付款：</span>
          <span class="norm_price">￥{{norm_price}}
            <!-- <span class="text">配送积分</span> -->
          </span>
        </div>
        <div class="operate" @click="exchange">
          <van-loading color="#fff" v-if="isLoading" />
          <span v-else>立即兑换</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import md5 from 'md5';
import { mapGetters } from 'vuex';
export default {
  name: 'orderConfirm',
  data () {
    return {
      value: 1,
      stock: 99,
      remark: '',
      isLoading: false,
      type: '1',
      goodsInfo: {},
      goods: {}
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'isPayPassword']),
    norm_price () {
      return this.$tools.floatObj.mul((this.goods && this.goods.norm_price) || 0, (this.goods && this.goods.num) || 0);
    },
    balance () {
      if (this.goods && this.goods.cate_area === 2) {
        return this.goods.norm_price > this.goodsInfo.integral ? this.$tools.floatObj.sub(this.norm_price, this.goodsInfo.integral) : 0;
      } else {
        return (this.goods && this.goods.norm_price) || 0;
      }
    },
    integral () {
      return this.norm_price > this.goodsInfo.integral ? this.goodsInfo.integral : this.norm_price;
    }
  },
  created () {
    this.goodsSubmit();
    this.$BUS.$on('password:pay', this.withdrawSecond);
  },
  mounted () {
    const query = this.$route.query;
    if (query && query.type) {
      this.$set(this, 'type', query.type);
    }
  },
  destroyed () {
    this.$BUS.$off('password:pay');
    sessionStorage.removeItem('address');
  },
  methods: {
    goodsSubmit () {
      this.$API.global.goodsSubmit({
        nid: this.$route.query.nid,
        num: this.$route.query.num
      }).then(res => {
        this.goodsInfo = res.data;
        this.goods = res.data.goods;
        var address = JSON.parse(sessionStorage.getItem('address'));
        this.address = address || res.data.address;
      });
    },
    goDetailsLink (id) {
      this.$router.push({ path: '/order/details', query: { id: id } });
    },
    getGoodsConfirm () {

    },
    withdrawSecond (data) {
      var query = {
        type: 2
      };
      var options = {
        nid: this.$route.query.nid,
        num: this.goods.num || this.$route.query.num,
        payMoney: this.balance,
        payIntegral: this.integral,
        payPass: md5(md5(data))
      };
      if (this.address && this.address.id) {
        options.addrId = this.address.id;
      }
      this.$API.global.goodsConfirm(options).then(res => {
        if (!res.code) {
          this.$store.dispatch('setPayPassword', false);
          setTimeout(() => {
            if (this.goodsInfo.goods.cate_area === 1) {
              query.orderNo = res.data;
            }
            this.$router.replace({ path: '/prompt', query: query });
          }, 200);
        } else {
          if (res.code === 111) {
            this.$dialog.alert({
              message: '<div class="goodsAlert">' + res.message + '</div>',
              confirmButtonText: '我知道了'
            });
          } else {
            this.$toast(res.message);
          }
        }
      });
    },
    exchange () {
      if (!this.userInfo.isSetPayPass) {
        this.$dialog.confirm({
          title: '温馨提醒',
          message: '您未设置支付密码，前去设置密码',
          confirmButtonText: '前去设置',
          cancelButtonText: '取消'
        }).then(() => {
          this.$router.push({ 'path': '/password/edit', query: { type: 2 } });
        }).catch(() => {
          // on cancel
        });
        return;
      }
      if (!this.address && this.goodsInfo && this.goodsInfo.goods && !this.goodsInfo.goods.is_virtual) {
        this.$toast('请先添加收货地址');
        return;
      }
      if (!this.isPayPassword) {
        this.$store.dispatch('setPayPassword', true);
        return;
      }
      this.isLoading = true;
    },
    goAddress () {
      this.$router.push({ 'path': '/address/list', query: { type: 'order' } });
    }
  }
};
</script>
<style lang="less" scoped>
.order-confirm {
  height: 100%;
  display: flex;
  flex-direction: column;
  .content {
    padding: 30px 0;
    flex: 1;
    overflow: auto;
    .address-box {
      width: 100%;
      background-color: #fff;
      display: flex;
      padding: 33px 50px;
      border-bottom: 1px solid #f7f6f6;
      align-items: center;
      position: relative;
      .detail {
        font-size: 28px;
        .name-phone {
          font-size: 28px;
          font-weight: 800;
          color: #353535;
          line-height: 52x;
        }
        .address {
          font-size: 24px;
          font-weight: 400;
          color: #666;
          line-height: 36x;
          margin-top: 13px;
        }
      }
      .next {
        display: inline-block;
        width: 16px;
        height: 30px;
        position: absolute;
        right: 40px;
        background-image: url('/static/images/icon-arrow-right.png');
        background-position: center center;
        background-repeat: no-repeat;
        background-size: 16px 30px;
      }
    }
    .product-box {
      width: 100%;
      background-color: #fff;
      padding: 30px 50px;
      .inventory {
        display: flex;
        margin-top: 40px;
        justify-content: space-between;
        .item-title {
          font-size: 28px;
          line-height: 62px;
        }
      }
      .product-info {
        display: flex;
        .imgPath {
          width: 180px;
          height: 180px;
          float: left;
        }
        .detail {
          margin-left: 30px;
          // height: 180px;
          width: 70%;
          overflow: hidden;
          display: inline-flex;
          flex-direction: column;
          .name {
            font-size: 30px;
            font-family: PingFang SC;
            font-weight: 800;
            color: #353535;
            line-height: 48px;
          }
          .spec {
            font-size: 24px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #999999;
            line-height: 30px;
            padding: 10px 0;
          }
        }
        .price-num {
          flex: 1;
          width: 100%;
          .price {
            color: #ff0034;
            font-size: 38px;
            .text {
              color: #ff0034;
              font-size: 24px;
              vertical-align: middle;
            }
          }
        }
      }
      .remark {
        margin-top: 36px;
        border-top: 2px solid #eeeeee;
        /deep/ .van-cell {
          padding: 25px 0;
        }
      }
    }
    .payaway-box {
      width: 100%;
      background-color: #fff;
      border-radius: 15px;
      padding: 30px 30px 0 30px;
      margin-top: 30px;
      .title {
        font-size: 34px;
        font-family: PingFang SC;
        font-weight: 800;
        color: #353535;
        line-height: 52px;
        padding-bottom: 30px;
        border-bottom: 1px solid #eeeeee;
        .tip {
          margin-left: 21px;
          font-size: 24px;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
          line-height: 48px;
        }
      }
      .away-list {
        padding: 20px 0;
        .away-item {
          height: 83px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .balance {
            flex: 1;
            font-size: 30px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #353535;
            line-height: 48px;
          }
          .amount {
            font-size: 30px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #ff0034;
            line-height: 48px;
          }
          img {
            width: 70px;
            height: 70px;
            margin-left: 35px;
          }
        }
      }
    }
  }
  .bt-box {
    width: 100%;
    height: 99px;
    background-color: #fff;
    display: flex;
    position: fixed;
    bottom: 0;
    justify-content: space-between;
    padding-left: 32px;
    .info {
      font-size: 28px;
      font-family: PingFang SC;
      font-weight: 500;
      line-height: 99px;
      span:nth-child(1) {
        color: #999999;
      }
      span:nth-child(2) {
        color: #353535;
      }
      .norm_price {
        font-size: 34px;
        color: #ff0034;
        .text {
          font-size: 24px;
          color: #ff0034;
          font-weight: normal;
          vertical-align: middle;
        }
      }
    }
    .operate {
      width: 200px;
      height: 72px;
      background-color: #007aff;
      font-size: 34px;
      font-family: PingFang SC;
      font-weight: normal;
      border-radius: 60px;
      color: #fff;
      margin-top: 14px;
      margin-right: 20px;
      line-height: 72px;
      text-align: center;
    }
  }
}
</style>
